home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / idlelib / PyParse.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  10KB  |  430 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import re
  5. import sys
  6. (C_NONE, C_BACKSLASH, C_STRING_FIRST_LINE, C_STRING_NEXT_LINES, C_BRACKET) = range(5)
  7. _synchre = re.compile('\n    ^\n    [ \\t]*\n    (?: while\n    |   else\n    |   def\n    |   return\n    |   assert\n    |   break\n    |   class\n    |   continue\n    |   elif\n    |   try\n    |   except\n    |   raise\n    |   import\n    |   yield\n    )\n    \\b\n', re.VERBOSE | re.MULTILINE).search
  8. _junkre = re.compile('\n    [ \\t]*\n    (?: \\# \\S .* )?\n    \\n\n', re.VERBOSE).match
  9. _match_stringre = re.compile('\n    \\""" [^"\\\\]* (?:\n                     (?: \\\\. | "(?!"") )\n                     [^"\\\\]*\n                 )*\n    (?: \\""" )?\n\n|   " [^"\\\\\\n]* (?: \\\\. [^"\\\\\\n]* )* "?\n\n|   \'\'\' [^\'\\\\]* (?:\n                   (?: \\\\. | \'(?!\'\') )\n                   [^\'\\\\]*\n                )*\n    (?: \'\'\' )?\n\n|   \' [^\'\\\\\\n]* (?: \\\\. [^\'\\\\\\n]* )* \'?\n', re.VERBOSE | re.DOTALL).match
  10. _itemre = re.compile('\n    [ \\t]*\n    [^\\s#\\\\]    # if we match, m.end()-1 is the interesting char\n', re.VERBOSE).match
  11. _closere = re.compile('\n    \\s*\n    (?: return\n    |   break\n    |   continue\n    |   raise\n    |   pass\n    )\n    \\b\n', re.VERBOSE).match
  12. _chew_ordinaryre = re.compile('\n    [^[\\](){}#\'"\\\\]+\n', re.VERBOSE).match
  13. _tran = [
  14.     'x'] * 256
  15. for ch in '({[':
  16.     _tran[ord(ch)] = '('
  17.  
  18. for ch in ')}]':
  19.     _tran[ord(ch)] = ')'
  20.  
  21. for ch in '"\'\\\n#':
  22.     _tran[ord(ch)] = ch
  23.  
  24. _tran = ''.join(_tran)
  25. del ch
  26.  
  27. try:
  28.     UnicodeType = type(unicode(''))
  29. except NameError:
  30.     UnicodeType = None
  31.  
  32.  
  33. class Parser:
  34.     
  35.     def __init__(self, indentwidth, tabwidth):
  36.         self.indentwidth = indentwidth
  37.         self.tabwidth = tabwidth
  38.  
  39.     
  40.     def set_str(self, str):
  41.         if not len(str) == 0 and str[-1] == '\n':
  42.             raise AssertionError
  43.         if type(str) is UnicodeType:
  44.             uniphooey = str
  45.             str = []
  46.             push = str.append
  47.             for raw in map(ord, uniphooey):
  48.                 if not raw < 127 or chr(raw):
  49.                     pass
  50.                 push('x')
  51.             
  52.             str = ''.join(str)
  53.         
  54.         self.str = str
  55.         self.study_level = 0
  56.  
  57.     
  58.     def find_good_parse_start(self, is_char_in_string = None, _synchre = _synchre):
  59.         str = self.str
  60.         pos = None
  61.         if not is_char_in_string:
  62.             return None
  63.         
  64.         limit = len(str)
  65.         for tries in range(5):
  66.             i = str.rfind(':\n', 0, limit)
  67.             if i < 0:
  68.                 break
  69.             
  70.             i = str.rfind('\n', 0, i) + 1
  71.             m = _synchre(str, i, limit)
  72.             if m and not is_char_in_string(m.start()):
  73.                 pos = m.start()
  74.                 break
  75.             
  76.             limit = i
  77.         
  78.         if pos is None:
  79.             m = _synchre(str)
  80.             if m and not is_char_in_string(m.start()):
  81.                 pos = m.start()
  82.             
  83.             return pos
  84.         
  85.         i = pos + 1
  86.         while None:
  87.             m = _synchre(str, i)
  88.             if m:
  89.                 (s, i) = m.span()
  90.                 if not is_char_in_string(s):
  91.                     pos = s
  92.                 
  93.             break
  94.             continue
  95.             return pos
  96.  
  97.     
  98.     def set_lo(self, lo):
  99.         if not lo == 0 and self.str[lo - 1] == '\n':
  100.             raise AssertionError
  101.         if lo > 0:
  102.             self.str = self.str[lo:]
  103.         
  104.  
  105.     
  106.     def _study1(self):
  107.         if self.study_level >= 1:
  108.             return None
  109.         
  110.         self.study_level = 1
  111.         str = self.str
  112.         str = str.translate(_tran)
  113.         str = str.replace('xxxxxxxx', 'x')
  114.         str = str.replace('xxxx', 'x')
  115.         str = str.replace('xx', 'x')
  116.         str = str.replace('xx', 'x')
  117.         str = str.replace('\nx', '\n')
  118.         continuation = C_NONE
  119.         level = lno = 0
  120.         self.goodlines = goodlines = [
  121.             0]
  122.         push_good = goodlines.append
  123.         i = 0
  124.         n = len(str)
  125.         while i < n:
  126.             ch = str[i]
  127.             i = i + 1
  128.             if ch == 'x':
  129.                 continue
  130.             
  131.             if ch == '\n':
  132.                 lno = lno + 1
  133.                 if level == 0:
  134.                     push_good(lno)
  135.                     continue
  136.                 continue
  137.             
  138.             if ch == '(':
  139.                 level = level + 1
  140.                 continue
  141.             
  142.             if ch == ')':
  143.                 if level:
  144.                     level = level - 1
  145.                     continue
  146.                 continue
  147.             
  148.             if ch == '"' or ch == "'":
  149.                 quote = ch
  150.                 if str[i - 1:i + 2] == quote * 3:
  151.                     quote = quote * 3
  152.                 
  153.                 firstlno = lno
  154.                 w = len(quote) - 1
  155.                 i = i + w
  156.                 while i < n:
  157.                     ch = str[i]
  158.                     i = i + 1
  159.                     if ch == 'x':
  160.                         continue
  161.                     
  162.                     if str[i - 1:i + w] == quote:
  163.                         i = i + w
  164.                         break
  165.                     
  166.                     if ch == '\n':
  167.                         lno = lno + 1
  168.                         if w == 0:
  169.                             if level == 0:
  170.                                 push_good(lno)
  171.                             
  172.                             break
  173.                             continue
  174.                         continue
  175.                     
  176.                     if ch == '\\':
  177.                         if not i < n:
  178.                             raise AssertionError
  179.                         if str[i] == '\n':
  180.                             lno = lno + 1
  181.                         
  182.                         i = i + 1
  183.                         continue
  184.                         continue
  185.                 if lno - 1 == firstlno:
  186.                     continuation = C_STRING_FIRST_LINE
  187.                     continue
  188.                 continuation = C_STRING_NEXT_LINES
  189.                 continue
  190.             
  191.             if ch == '#':
  192.                 i = str.find('\n', i)
  193.                 if not i >= 0:
  194.                     raise AssertionError
  195.                 continue
  196.             
  197.             if not ch == '\\':
  198.                 raise AssertionError
  199.             if not i < n:
  200.                 raise AssertionError
  201.             if str[i] == '\n':
  202.                 lno = lno + 1
  203.                 if i + 1 == n:
  204.                     continuation = C_BACKSLASH
  205.                 
  206.             
  207.             i = i + 1
  208.         if continuation != C_STRING_FIRST_LINE and continuation != C_STRING_NEXT_LINES and level > 0:
  209.             continuation = C_BRACKET
  210.         
  211.         self.continuation = continuation
  212.         if not continuation == C_NONE == goodlines[-1] == lno:
  213.             raise AssertionError
  214.         if goodlines[-1] != lno:
  215.             push_good(lno)
  216.         
  217.  
  218.     
  219.     def get_continuation_type(self):
  220.         self._study1()
  221.         return self.continuation
  222.  
  223.     
  224.     def _study2(self):
  225.         if self.study_level >= 2:
  226.             return None
  227.         
  228.         self._study1()
  229.         self.study_level = 2
  230.         str = self.str
  231.         goodlines = self.goodlines
  232.         i = len(goodlines) - 1
  233.         p = len(str)
  234.         while i:
  235.             if not p:
  236.                 raise AssertionError
  237.             q = p
  238.             for nothing in range(goodlines[i - 1], goodlines[i]):
  239.                 p = str.rfind('\n', 0, p - 1) + 1
  240.             
  241.             if _junkre(str, p):
  242.                 i = i - 1
  243.                 continue
  244.             break
  245.         if i == 0:
  246.             if not p == 0:
  247.                 raise AssertionError
  248.             q = p
  249.         
  250.         self.stmt_start = p
  251.         self.stmt_end = q
  252.         lastch = ''
  253.         stack = []
  254.         push_stack = stack.append
  255.         bracketing = [
  256.             (p, 0)]
  257.         while p < q:
  258.             m = _chew_ordinaryre(str, p, q)
  259.             if m:
  260.                 newp = m.end()
  261.                 i = newp - 1
  262.                 while i >= p and str[i] in ' \t\n':
  263.                     i = i - 1
  264.                 if i >= p:
  265.                     lastch = str[i]
  266.                 
  267.                 p = newp
  268.                 if p >= q:
  269.                     break
  270.                 
  271.             
  272.             ch = str[p]
  273.             if ch in '([{':
  274.                 push_stack(p)
  275.                 bracketing.append((p, len(stack)))
  276.                 lastch = ch
  277.                 p = p + 1
  278.                 continue
  279.             
  280.             if ch in ')]}':
  281.                 if stack:
  282.                     del stack[-1]
  283.                 
  284.                 lastch = ch
  285.                 p = p + 1
  286.                 bracketing.append((p, len(stack)))
  287.                 continue
  288.             
  289.             if ch == '"' or ch == "'":
  290.                 bracketing.append((p, len(stack) + 1))
  291.                 lastch = ch
  292.                 p = _match_stringre(str, p, q).end()
  293.                 bracketing.append((p, len(stack)))
  294.                 continue
  295.             
  296.             if ch == '#':
  297.                 bracketing.append((p, len(stack) + 1))
  298.                 p = str.find('\n', p, q) + 1
  299.                 if not p > 0:
  300.                     raise AssertionError
  301.                 bracketing.append((p, len(stack)))
  302.                 continue
  303.             
  304.             if not ch == '\\':
  305.                 raise AssertionError
  306.             p = p + 1
  307.             if not p < q:
  308.                 raise AssertionError
  309.             if str[p] != '\n':
  310.                 lastch = ch + str[p]
  311.             
  312.             p = p + 1
  313.         self.lastch = lastch
  314.         if stack:
  315.             self.lastopenbracketpos = stack[-1]
  316.         
  317.         self.stmt_bracketing = tuple(bracketing)
  318.  
  319.     
  320.     def compute_bracket_indent(self):
  321.         self._study2()
  322.         if not self.continuation == C_BRACKET:
  323.             raise AssertionError
  324.         j = self.lastopenbracketpos
  325.         str = self.str
  326.         n = len(str)
  327.         origi = i = str.rfind('\n', 0, j) + 1
  328.         j = j + 1
  329.         while j < n:
  330.             m = _itemre(str, j)
  331.             if m:
  332.                 j = m.end() - 1
  333.                 extra = 0
  334.                 break
  335.                 continue
  336.             i = j = str.find('\n', j) + 1
  337.         j = i = origi
  338.         while str[j] in ' \t':
  339.             j = j + 1
  340.         extra = self.indentwidth
  341.         return len(str[i:j].expandtabs(self.tabwidth)) + extra
  342.  
  343.     
  344.     def get_num_lines_in_stmt(self):
  345.         self._study1()
  346.         goodlines = self.goodlines
  347.         return goodlines[-1] - goodlines[-2]
  348.  
  349.     
  350.     def compute_backslash_indent(self):
  351.         self._study2()
  352.         if not self.continuation == C_BACKSLASH:
  353.             raise AssertionError
  354.         str = self.str
  355.         i = self.stmt_start
  356.         while str[i] in ' \t':
  357.             i = i + 1
  358.         startpos = i
  359.         endpos = str.find('\n', startpos) + 1
  360.         found = level = 0
  361.         while i < endpos:
  362.             ch = str[i]
  363.             if ch in '([{':
  364.                 level = level + 1
  365.                 i = i + 1
  366.                 continue
  367.             if ch in ')]}':
  368.                 if level:
  369.                     level = level - 1
  370.                 
  371.                 i = i + 1
  372.                 continue
  373.             if ch == '"' or ch == "'":
  374.                 i = _match_stringre(str, i, endpos).end()
  375.                 continue
  376.             if ch == '#':
  377.                 break
  378.                 continue
  379.             if level == 0 and ch == '=':
  380.                 if (i == 0 or str[i - 1] not in '=<>!') and str[i + 1] != '=':
  381.                     found = 1
  382.                     break
  383.                     continue
  384.             i = i + 1
  385.         if found:
  386.             i = i + 1
  387.             found = re.match('\\s*\\\\', str[i:endpos]) is None
  388.         
  389.         if not found:
  390.             i = startpos
  391.             while str[i] not in ' \t\n':
  392.                 i = i + 1
  393.         
  394.         return len(str[self.stmt_start:i].expandtabs(self.tabwidth)) + 1
  395.  
  396.     
  397.     def get_base_indent_string(self):
  398.         self._study2()
  399.         i = self.stmt_start
  400.         n = self.stmt_end
  401.         j = i
  402.         str = self.str
  403.         while j < n and str[j] in ' \t':
  404.             j = j + 1
  405.         return str[i:j]
  406.  
  407.     
  408.     def is_block_opener(self):
  409.         self._study2()
  410.         return self.lastch == ':'
  411.  
  412.     
  413.     def is_block_closer(self):
  414.         self._study2()
  415.         return _closere(self.str, self.stmt_start) is not None
  416.  
  417.     lastopenbracketpos = None
  418.     
  419.     def get_last_open_bracket_pos(self):
  420.         self._study2()
  421.         return self.lastopenbracketpos
  422.  
  423.     stmt_bracketing = None
  424.     
  425.     def get_last_stmt_bracketing(self):
  426.         self._study2()
  427.         return self.stmt_bracketing
  428.  
  429.  
  430.